home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Games Collection 1
/
software vault.zip
/
software vault
/
CDR10
/
TGE129C.ZIP
/
SOURCE
/
TGEFONT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-20
|
5KB
|
196 lines
/*****************************************************************************
* The Graphics Engine version 1.29ßC *
* *
* The Graphics Engine code and documentation are Copyright (c) 1993 *
* by Matthew Hildebrand. *
* *
* Unauthorised usage or modification of any or all of The Graphics *
* Engine is strictly prohibited. *
*****************************************************************************/
#include <alloc.h>
#include <dir.h>
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include "tgefont.h"
#include "tge.h"
//*****
//***** Constructor
//*****
Font::Font(char *filename, unsigned char fg, unsigned char bg)
{
unsigned char huge *tempBuf, *in, *out;
char signature[8];
unsigned long fileLen;
unsigned count1, count2;
FILE *inFile;
unsigned char byte;
//*** Initialize member variables
rawData = NULL;
fgColour = fg;
bgColour = bg;
//*** Open font file
if ((inFile=fopen(filename,"rb")) == NULL)
return;
//*** Parse header
if (!fread(signature, 8, 1, inFile)) // read in signature
return;
if (memcmp(signature, "TGEFONT1", 8)) // is signature there?
return; // no, quit
if (!fread(&charWide, 2, 1, inFile)) // read in character width
return;
if (!fread(&charDeep, 2, 1, inFile)) // read in character depth
return;
charSize = charWide * charDeep; // precalculate for speed
//*** Ensure format correct (ie., file size is charWide*charDeep+12)
if (fseek(inFile, 0L, SEEK_END)) // seek to end of file
return;
fileLen=ftell(inFile); // get file length
if (fseek(inFile, 12L, SEEK_SET)) // seek past header
return;
fileLen -= 12L; // remove header length
if (fileLen != (charSize/8)*256L) // verify size OK
return;
//*** Read in character data
(void far*)tempBuf = farmalloc(fileLen); // allocate some RAM
if (tempBuf == NULL) // was function successful?
return; // no, quit
if (!fread(tempBuf, (unsigned)fileLen, 1, inFile)) // read in font data
{ // clean up and quit on error
farfree(tempBuf);
return;
}
fclose(inFile); // close the file
//*** Allocate required memory blocks
rawData = farmalloc((long)charSize * 256L); // bitmap space
if (rawData == NULL) // was function successful?
{ // no, clean up and quit
farfree(tempBuf);
return; // no, quit
}
image = farmalloc(charSize + 4L); // allocate the image buffer
if (image == NULL) // was function successful?
{ // no, clean up and quit
farfree(rawData);
farfree(tempBuf);
return;
}
else
{
((unsigned*)image)[0] = charWide; // fill in dimensions
((unsigned*)image)[1] = charDeep;
}
//*** Convert font data from 1 bit/pixel to 1 byte/pixel
(void far*)in = tempBuf; // initialize pointers
(void far*)out = rawData;
for (count1=0; count1<fileLen; count1++) // conversion loop
{
byte = *in; // get current byte
*out = (byte>>7) & 1; // bits --> bytes
out++;
*out = (byte>>6) & 1;
out++;
*out = (byte>>5) & 1;
out++;
*out = (byte>>4) & 1;
out++;
*out = (byte>>3) & 1;
out++;
*out = (byte>>2) & 1;
out++;
*out = (byte>>1) & 1;
out++;
*out = (byte>>0) & 1;
out++;
in++; // update pointer
}
farfree(tempBuf); // free temporary memory
//*** Fill in character address array
(void far*)out = rawData; // initialize pointer
for (count2=0; count2<=255; count2++) // loop for each character
{
addrArray[count2] = out; // fill in current element
out += charSize; // update address
}
}
//*****
//***** Destructor
//*****
Font::~Font(void)
{
if (rawData) // was memory allocated?
farfree(rawData); // yes, free it
if (image) // same procedure
farfree(image);
}
//*****
//***** Compute the width, in pixels, of the given string
//*****
unsigned Font::wide(char *str)
{
return (charWide * strlen(str));
}
//*****
//***** Write a string at the specified location
//*****
void Font::put(int x, int y, char *str)
{
unsigned count, length;
length = strlen(str); // store for speed
for (count=0; count<length; count++) // loop for each character
put(x+count*charWide, y, str[count]); // put the character
}
//*****
//***** Write a single character at the specified location
//*****
void Font::put(int x, int y, char ch)
{
register unsigned count;
unsigned char far *in, *out;
(void far*)in = addrArray[(unsigned char)ch]; // initialize temp pointers
(void far*)out = image; // pointer to image buffer
out += 4; // bump past dimension info
for (count=0; count<charSize; count++) // loop for each pixel
{
if (*in == 0) // is it background colour?
*out = bgColour; // yes
else
*out = fgColour; // no, must be foreground
in++; // update pointers
out++;
}
putImageInv(x, y, image); // draw the character
}